<template>
	<view class="page">
		<view class="main">
			<u-search placeholder="请输入搜索内容" v-model="keyword" height="68rpx" bgColor="#F4F6F8"
				:showAction="false"></u-search>
			<view class="swiper-wrap">
				<u-swiper :height="88" :list="swiperList" @click="clickSwiper" keyName="img"></u-swiper>
			</view>
			<view class="classify-wrap">
				<view class="item" v-for="i in productTypeList" @click="changeType(i.id)">
					<image class="icon" :src="fileServer+i.typeImg" mode="aspectFill"></image>
					<view class="label">
						{{i.typeName}}
					</view>
				</view>
			</view>
			<view class="classify-type">
				<u-scroll-list :indicator="false">
					<view class="item" :class="productType1 === item.id?'active':''"
						v-for="(item, index) in productTypeList1" :key="index" @click="changeType1(item.id)">
						{{item.typeName}}
					</view>
				</u-scroll-list>
			</view>
			<view class="adv-wrap">
				<view class="advItem" @click="goRanking">
					<image class="adv" src="../../../static/img/shop1.png" mode="aspectFill"></image>
					<!-- <view class="text">
						<view class="h">获客排行</view>
						<text class="p">推广获客</text>
					</view> -->
				</view>
				<view class="advItem" @click="goList(1)">
					<image class="adv" src="../../../static/img/shop2.png" mode="aspectFill"></image>
					<!-- <view class="text">
						<view class="h">9.9促销</view>
						<text class="p">爆品特卖</text>
					</view> -->
				</view>
				<view class="advItem" @click="goList(2)">
					<image class="adv" src="../../../static/img/shop3.png" mode="aspectFill"></image>
					<!-- <view class="text">
						<view class="h">大牌专区</view>
						<text class="p">优质产品</text>
					</view> -->
				</view>
			</view>
			<view class="select-type">
				<view class="select" @click="regionShow = true">
					<view class="text">{{queryParams.province||'招商区域'}}</view>
					<image class="icon" src="../../../static/img/icon-down.png" mode="widthFix"></image>
				</view>
				<view class="select" @click="profitShow = !profitShow;priceShow = false">
					<view class="text">利润率</view>
					<image class="icon" src="../../../static/img/icon-down.png" mode="widthFix"></image>
				</view>
				<view class="select" @click="priceShow = !priceShow;profitShow = false">
					<view class="text">价格带</view>
					<image class="icon" src="../../../static/img/icon-down.png" mode="widthFix"></image>
				</view>
				<view class="select" @click="provincesShow = true">
					<view class="text">{{queryParams.chandi||'产地'}}</view>
					<image class="icon" src="../../../static/img/icon-down.png" mode="widthFix"></image>
				</view>
				<!-- 利润率 -->
				<view class="selectCont" v-if="profitShow">
					<view class="selectform">
						<u--input type="digit"
						    placeholder="请输入最小值" v-model="profitInfo.min"
						    clearable
						  ></u--input>
						<text style="padding:0 10rpx;">~</text>
						<u--input type="digit"
						  placeholder="请输入最大值"  v-model="profitInfo.max"
						  clearable
						>
						</u--input>
					</view>
					<view class="btns">
						<view class="btn">
							<u-button @click="profitBtn('0')" type="warning" shape="circle" text="重置"></u-button>
						</view>
						<view class="btn">
							<u-button @click="profitBtn('1')" class="btn" type="primary" shape="circle" text="确定"></u-button>
						</view>
					</view>
				</view>
				<!-- 价格带 -->
				<view class="selectCont" v-if="priceShow">
					<view class="selectform">
						<u--input type="digit"
						    placeholder="请输入最小值" v-model="priceInfo.min"
						    clearable
						  ></u--input>
						<text style="padding:0 10rpx;">~</text>
						<u--input type="digit"
						  placeholder="请输入最大值"  v-model="priceInfo.max"
						  clearable
						>
						</u--input>
					</view>
					<view class="btns">
						<view class="btn">
							<u-button @click="priceBtn('0')" type="warning" shape="circle" text="重置"></u-button>
						</view>
						<view class="btn">
							<u-button @click="priceBtn('1')" class="btn" type="primary" shape="circle" text="确定"></u-button>
						</view>
					</view>
				</view>
			</view>
			<view class="goods-wrap">
				<view class="goods-left">
					<view class="goods-pic">
						<image class="pic" mode="aspectFill" :src="fileServer+item.productImg" v-for="(item,i) in group1" :key="i" @click="toDetail(item)"></image>
					</view>
					<view class="goods" v-for="(item,x) in oddGroup" :key="x" @click="toDetail(item)">
						<image class="goods-img" mode="aspectFill" :src="fileServer+item.productImg"></image>
						<view class="name">
							{{item.productName}}
						</view>
						<view class="price">
							<view class="text">
								<view class="red">
									{{item.mll}}
								</view>
								<view class="t">
									毛利
								</view>
							</view>
							<view class="text">
								<view class="t">
									零售
								</view>
								<view class="red">
									{{item.price}}
								</view>
							</view>
							<view class="text">
								<view class="t">
									产地{{item.chandi}}
								</view>
							</view>
						</view>
					</view>
				</view>
				<view class="goods-rit">
					<view class="goods" v-for="item in evenGroup">
						<image class="goods-img"  mode="aspectFill" :src="fileServer+item.productImg"></image>
						<view class="name">
							{{item.productName}}
						</view>
						<view class="price">
							<view class="text">
								<view class="red">
									{{item.mll}}
								</view>
								<view class="t">
									毛利
								</view>
							</view>
							<view class="text">
								<view class="t">
									零售
								</view>
								<view class="red">
									{{item.price}}
								</view>
							</view>
							<view class="text">
								<view class="t">
									产地{{item.chandi}}
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!--招商区域 regionList -->
		<u-picker :show="regionShow" :columns="regionList" @cancel="regionShow = false"
			@confirm="chooseRegion"></u-picker>
		<!-- 产地 -->
		<u-picker :show="provincesShow" :columns="provinces" @cancel="provincesShow = false"
			@confirm="chooseProvinces"></u-picker>
		
		
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				swiperList: [],
				keyword: '',
				productTypeList: [],
				productTypeList1: [],
				productType: null,
				productType1: null,
				fileServer: this.$fileServer,
				pageIndex: 1,
				lists: [],
				group1: [],
				oddGroup: [],
				evenGroup: [],
				queryParams: {
					chandi:'',//产地
					lrlMin:'',//利润率开始
					lrlMax:'',//利润率结束
					priceMin:'',//价格带开始
					priceMax:'',//
					productTypeId:'',//行业分类ID：0--查询公司全部产品；传ID获取该分类下的产品
					province:'',//招商区域
				},
				provinces: getApp().globalData.provincesList,//产地
				provincesShow: false,
				regionList:getApp().globalData.regionList,//招商区域
				regionShow:false,
				profitShow:false,
				profitInfo:{
					min:'',
					max:''
				},
				priceShow:false,
				priceInfo:{
					min:'',
					max:''
				},
			};
		},
		onLoad() {
			this.getBanner();
			this.getProductTypeList()
			this.getList()
		},
		methods: {
			// 排行榜
			goRanking(){
				uni.navigateTo({
					url:'/pages/shop/ranking/ranking'
				})
			},
			// 去list
			goList(type){
				uni.navigateTo({
					url:'/pages/shop/list/list?type='+type
				})
			},
			// 价格带
			priceBtn(type){
				console.log(type)
				if(type==1){//确认
					this.priceShow = false;
					this.queryParams.priceMin = this.priceInfo.min;
					this.queryParams.priceMax = this.priceInfo.max;
					this.pageIndex = 1;
					this.getList()
				}else{
					this.priceInfo = {
						min:'',
						max:''
					}
				}
			},
			// 利润率
			profitBtn(type){
				console.log(type)
				if(type==1){//确认
					this.profitShow = false;
					this.queryParams.lrlMin = this.profitInfo.min;
					this.queryParams.lrlMax = this.profitInfo.max;
					this.pageIndex = 1;
					this.getList()
					
				}else{
					this.profitInfo = {
						min:'',
						max:''
					}
				}
			},
			// 招商区域
			chooseRegion(e) {
				console.log(e)
				this.queryParams.province = e.value[0]
				this.regionShow = false;
				this.pageIndex = 1;
				this.getList()
			},
			// 选择产地
			chooseProvinces(e) {
				console.log(e)
				this.queryParams.chandi = e.value[0]
				this.provincesShow = false;
				this.pageIndex = 1;
				this.getList()
			},
			getBanner(){
				this.$api.member.getBannerList({type:0}).then(res => {
					if(res.flag){
						let list = res.data;
						list.forEach(ele=>{
							ele.img = this.fileServer +ele.img;
						})
						this.swiperList =list;
					}
				})
			},
			groupArray(array, groupSize) {
				const groupedArray = [];
				for (let i = 0; i < array.length; i += groupSize) {
					groupedArray.push(array.slice(i, i + groupSize));
				}
				return groupedArray;
			},
			getList() {
				let params = {...this.queryParams};
				params.pageIndex = this.pageIndex;
				params.productTypeId = this.productType1;
				this.$api.company.getProductList(params).then(res => {
					uni.stopPullDownRefresh(); // 关闭下拉刷新 
					if (res.rows.length > 0) {
						this.lists = this.lists.concat(res.rows)
						let list = JSON.parse(JSON.stringify(this.lists))
						const group1 = list.slice(0, 4);
						this.group1 = group1
						if (list.length > 4) {
							const remainingProducts = list.slice(4);
							const oddGroup = remainingProducts.filter((x, index) => index % 2 !== 0);
							const evenGroup = remainingProducts.filter((x, index) => index % 2 === 0);
							this.oddGroup = oddGroup||[];
							this.evenGroup = evenGroup||[];
						}
					} else {
						--this.pageIndex
					}
				})
			},
			changeType(type) {
				this.productType = type
				this.$api.company.getProductTypeList({
					productTypeId: this.productType
				}).then(res => {
					this.productTypeList1 = res.data
				})

			},
			changeType1(type) {
				this.productType1 = type
				this.lists = []
				this.pageIndex = 1
				this.getList()
			},
			getProductTypeList() {
				this.$api.company.getProductTypeList({
					productTypeId: 0
				}).then(res => {
					this.productTypeList = res.data
				})
			},
			clickSwiper() {},
			toDetail(item) {
				console.log(item);
				let id = item.id;
				uni.navigateTo({
					url: "/pages/shop/detail/detail?id="+id
				})
			},
		},
		onPullDownRefresh () {
		  this.getList()
		},
	}
</script>

<style lang="scss" scoped>
	.page {
		background: linear-gradient(to bottom, #ffffff, #f5f5f5);min-height: 100vh;
		// .popupCont{position: relative;padding: 24rpx;width: 375rpx;box-sizing: border-box;height: 100%;
		// 	.tit{font-size: 30rpx;color: #333;font-weight: bold;margin-bottom: 30rpx;}
		// 	.btns{position: absolute;left: 24rpx;right: 24rpx;bottom: 24rpx;display: flex;justify-content: space-between;
		// 		.btn{width: 48%;}
		// 	}
		// }
		.main {
			padding: 20rpx 32rpx;
			width: 100%;

			.swiper-wrap {
				margin-top: 24rpx;
			}

			.classify-wrap {
				margin-top: 20rpx;
				width: 100%;
				background: #FFFFFF;
				border-radius: 18rpx;
				padding: 20rpx 20rpx 0 18rpx;
				overflow: hidden;

				.item {
					width: 114rpx;
					margin-right: 18rpx;
					margin-bottom: 14rpx;
					float: left;

					.icon {
						width: 114rpx;
						height: 114rpx;
						margin-bottom: 8rpx;
					}

					.label {
						font-size: 28rpx;
						text-align: center;
					}

					&:nth-child(5n) {
						margin-right: 0;
					}
				}
			}

			.classify-type {
				margin-top: 24rpx;

				.item {
					width: auto;
					height: 56rpx;
					background: #EBEBEB;
					border-radius: 28rpx;
					padding: 0 12rpx;
					display: inline-block;
					margin-right: 20rpx;
					font-size: 28rpx;
					white-space: nowrap;
					line-height: 56rpx;

					&.active {
						background: #EC2424;
						color: #fff;
					}
				}
			}

			.adv-wrap {
				width: 100%;
				display: flex;
				align-items: center;
				justify-content: space-between;
				.advItem{position: relative;width: 216rpx;height: 160rpx;
					.text{position: absolute;top: 0;left: 0;padding: 16rpx;
						.h{color: #333333;font-size: 32rpx;font-weight: bold;margin-bottom: 10rpx;}
						.p{color: #333333;font-size: 24rpx;}
					}
				}
				.adv {
					width: 216rpx;
					height: 160rpx;
					border-radius: 16rpx;
				}
			}

			.select-type {
				width: 100%;
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-top: 24rpx;
				position: relative;

				.select {
					display: flex;
					align-items: center;
					.text {
						max-width: 150rpx;
						color: #333;
						font-size: 32rpx;margin-right: 5rpx;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;
					}

					.icon {
						width: 22rpx;
						height: auto;
					}
				}
				.selectCont{position: absolute;left: 0;top: 45rpx;right: 0;background: #fff;padding: 24rpx;box-shadow: 0 5 50rpx #999;
					.selectform{display: flex;align-items: center;}
					.btns{display: flex;align-items: center;justify-content: space-between;margin-top: 30rpx;
						.btn{width: 48%;}
					}
				}
			}

			.goods-wrap {
				width: 100%;
				display: flex;
				margin-top: 24rpx;

				.goods-left,
				.goods-rit {
					width: 334rpx;
				}

				.goods-left {
					margin-right: 16rpx;
				}

				.goods-pic {
					width: 100%;
					overflow: hidden;
					margin-bottom: 8rpx;

					.pic {
						width: 160rpx;
						height: 160rpx;
						border-radius: 16rpx;
						margin-right: 14rpx;
						margin-bottom: 16rpx;

						&:nth-child(2n+0) {
							margin-right: 0;
						}
					}
				}

				.goods {
					margin-bottom: 24rpx;
					width: 100%;
					background: #FFFFFF;
					border-radius: 16rpx;
					overflow: hidden;
					padding-bottom: 20rpx;

					.goods-img {
						width: 100%;
						height: 334rpx;
					}

					.name {
						margin-top: 16rpx;
						font-size: 28rpx;
						line-height: 40rpx;
						padding: 0 12rpx;
					}

					.price {
						display: flex;
						align-items: center;
						justify-content: space-between;
						padding: 0 12rpx;
						margin-top: 10rpx;

						.text {
							display: flex;
							align-items: center;

							.red {

								color: #E83149;
								font-size: 28rpx;
							}

							.t {

								font-size: 22rpx;
							}
						}
					}

				}
			}
		}
	}
</style>